package cn.syx.comtroller;

import cn.syx.service.function.DateService;
import cn.syx.service.function.WeekService;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.Generation;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.function.FunctionCallbackWrapper;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DateFunctionController {

    @Autowired
    private ChatClient chatClient;

    @GetMapping("/dateFunction")
    public String dateFunction(@RequestParam String message){
        // 指定调用的 工具函数：OpenAiChatOptions.builder().withFunction("dateService")，dateService：工具函数 bean 的 name
        Prompt prompt = new Prompt(message,
                OpenAiChatOptions.builder().withFunction("dateService").build());
        Generation result = chatClient.call(prompt).getResult();
        if(result != null){
            return result.getOutput().getContent();
        } else {
            return "";
        }
    }

    @GetMapping("/dateFunctionCallBack")
    public String dateFunctionCallBack(@RequestParam String message){
        //构造方法 Prompt(String contents, ChatOptions modelOptions)
        Prompt prompt = new Prompt(message,
                OpenAiChatOptions.builder().withFunctionCallbacks(
                        List.of(
                        FunctionCallbackWrapper.builder(new DateService())
                        .withName("dateService")
                        .withDescription("获取指定地点的当前时间").build()
                        )
                ).build());

        Generation result = chatClient.call(prompt).getResult();
        if(result != null){
            return result.getOutput().getContent();
        } else {
            return "";
        }
    }

    @GetMapping("/dateFunctionCallBack")
    public String dateFunctionCallBack2(@RequestParam String message){
        //系统提示词
        SystemMessage systemMessage = new SystemMessage("请用中文回答我");
        //用户提示词
        UserMessage userMessage = new UserMessage(message);
        // 构造方法 Prompt(List<Message> messages, ChatOptions modelOptions)
        Prompt prompt = new Prompt(List.of(systemMessage,userMessage),
                OpenAiChatOptions.builder().withFunctionCallbacks(
                        List.of(
                                FunctionCallbackWrapper.builder(new DateService())
                                        .withName("dateService")
                                        .withDescription("获取指定地点的当前时间").build(),
                                FunctionCallbackWrapper.builder(new WeekService())
                                        .withName("weekService")
                                        .withDescription("获取指定地点的当前星期几").build()
                        )
                ).build()
        );

        Generation result = chatClient.call(prompt).getResult();
        if(result != null){
            return result.getOutput().getContent();
        } else {
            return "";
        }
    }
}
